<?php
namespace App\Http\Controllers\Saas;

use App\Http\Config\ErrorCode;
use App\Models\Admin\AdminUser;
use App\Models\Saas\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Qcloud\Sms\SmsSingleSender;

class AttachController extends BaseController
{
    public function test(){

    }

    //站点信息
    public function index(){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $laravel = app();
        $pach = public_path().'/version.txt';
        $edition = file_get_contents($pach);  //获取版本信息
        $arr[] = array('con_name'=>'程序版本','con_value'=>'云运维管理系统  ' . 'v'.$edition);
        $wxapp = DB::table('wxapp_upgrade')->orderBy('create_time','desc')->select('version')->first();
        $arr[] = array('con_name'=>'小程序版本','con_value'=>'微购儿CRM  ' . 'v'.(!empty($wxapp->version) ? $wxapp->version : "0"));
        global $scf_data;
        if (empty($scf_data["IS_SCF"])){
            $arr[] = array('con_name'=>'服务器IP地址','con_value'=>$_SERVER['SERVER_ADDR']);
            $arr[] = array('con_name'=>'服务器域名','con_value'=>$_SERVER['SERVER_NAME']);
            $arr[] = array('con_name'=>'服务器端口','con_value'=>$_SERVER['SERVER_PORT']);
        }
        $arr[] = array('con_name'=>'服务器版本','con_value'=>php_uname('s').php_uname('r'));
        $arr[] = array('con_name'=>'服务器操作系统','con_value'=>php_uname());
        $arr[] = array('con_name'=>'PHP版本','con_value'=>phpversion());
        $arr[] = array('con_name'=>'Laravel版本','con_value'=>$laravel::VERSION);
        $arr[] = array('con_name'=>'服务器当前时间 ','con_value'=>date("Y-m-d H:i:s"));
        $arr[] = array('con_name'=>'最大上传限制 ','con_value' => get_cfg_var ('upload_max_filesize') ? get_cfg_var ('upload_max_filesize'):'不允许');
        $arr[] = array('con_name'=>'最大执行时间','con_value'=>get_cfg_var("max_execution_time")."秒 ");
        $arr[] = array('con_name'=>'数据库版本','con_value'=>'Mysql 5.7');
        $arr[] = array('con_name' => '软件版本', 'con_value' => 'V3.4');
        $arr[] = array('con_name' => '联系我们', 'con_value' => '手机/微信：13670093216 QQ：1018608475');
        $arr[] = array('con_name' => '版权所有', 'con_value' => '深圳市网商天下科技开发有限公司');
        $arr[] = array('con_name' => '总策划兼项目经理', 'con_value' => '车贯 （Michael）');
        $arr[] = array('con_name' => '研发团队', 'con_value' => '陈元  许堉颖  陈镇松  李迪  杜新科  刘丹');
        $arr[] = array('con_name' => '设计团队', 'con_value' => '龙芳 林鸿耿');
        $arr[] = array('con_name' => '特别支持', 'con_value' => '刘远航  袁程远');
        $this->returnData['data'] = $arr;
        return $this->return_result($this->returnData);
    }

    public function sendCode(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(empty($request['mobile'])){
            return $this->return_result(ErrorCode::code("params_not_exist", "手机号"));
        }
        $settingData = Setting::getFields("sms_id,sms_secret,sms_sign,sms_code");
        if(empty($settingData) || !$settingData["smsId"] || !$settingData["smsSecret"] || !$settingData["smsCode"]){
            return $this->return_result(ErrorCode::code("customized", "短信未配置"));
        }
        $preg_phone = '/^1[3456789]\d{9}$/';
        if(!preg_match($preg_phone, $request["mobile"])){
            return $this->return_result(ErrorCode::code("customized", "手机号格式错误"));
        }
        $validate_res = DB::table('verification')->where(
            [["mall_id", '=', 0], ["mobile", "=", $request['mobile']], ["overdue_time", ">=", time() - 60]]
        )->first();
        if($validate_res){
            return $this->return_result(ErrorCode::code("customized", "一分钟内请勿频繁获取验证码"));
        }
        try {
            $code = rand(1000, 9999);
            $sender = new SmsSingleSender($settingData["smsId"], $settingData["smsSecret"]);
            $result = $sender->sendWithParam("86", $request['mobile'], $settingData["smsCode"],
                [$code], $settingData["smsSign"], "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            $rsp = json_decode($result,true);
            if ($rsp['result'] == 0){
                $this->returnData['msg'] = '短信发送成功';
                DB::table('verification')->insert(["mobile" => $request["mobile"], "code" => $code, "create_time" => time(), "overdue_time" => time()+60]);
            }else{
                $this->returnData = ErrorCode::$admin_enum["fail"];
                $this->returnData['msg'] = '短信发送失败';
            }

        } catch(\Exception $e) {
            $this->returnData = ErrorCode::code("customized", $e->getMessage());
        }
        return $this->return_result($this->returnData);
    }
}
